(function(){
    var BaeImage = require('baev3-image');
    var BaeImageQRCode = BaeImage.QRCode;
    var BaeImageConstant = BaeImage.Constant;
    var BaeImageService = BaeImage.Service;
    //Image服务连接信息，可查询Image服务详情页
    var hostname = 'image.duapp.com'
    var ak = 'usEmwoOIBOdXekAAbjfSwD0o';
    var sk = 'g5HCEf0uvvdK35sPuGySD2oATzDjGGNZ';
    var imageService = new BaeImageService(ak, sk, hostname);
    function testQRCode(req, res) {

        var imageQRCode = new BaeImageQRCode();

        // var text = (new Buffer('welcome to bae')).toString('base64')
        var text = req.query.text;
        if(!text || typeof text !== "string"){
            res.end('text is not empty!');
            return;
        }
        imageQRCode.setText(text);
        imageQRCode.setVersion(2);
        imageQRCode.setSize(10);
        imageQRCode.setLevel(3);
        imageQRCode.setMargin(1);
        imageQRCode.setForeground('000000');
        imageQRCode.setBackground('FFFFFF');

        imageService.applyQRCodeByObject(imageQRCode, function(err, result) {
            if (err) {
                console.log(err);
                res.end('transform error');
                return;
            }

            var data = new Buffer(result['response_params'].image_data, 'base64');
            res.writeHead(200, {'Content-Type': 'image/jpg' });
            res.end(data);
        });
    }
    module.exports = {
        QRCode: testQRCode
    };
})();